﻿Shader "Custom/Alliance_Flag" {
	Properties {
//		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_AlphaTex("Alpha Texture", 2D) = "white" {}

		_Diffuse ("Diffuse Factor", Range(1, 2)) = 1
 
		_Frequency("_Frequency", Float) = 1
		_Amplitude("_Amplitude", Float) = 1
		_Speed("_Speed", Float) = 1

//		_uvMaxY("uvMaxY", Range(0, 1)) = 1
		_waveFactor("wave factor", Float) = 1
	}
	SubShader {
        Tags {"Queue"="Transparent+110" "IgnoreProjector"="True" "RenderType"="Transparent"}
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert vertex:vert alpha:fade

		sampler2D _MainTex;
		sampler2D _AlphaTex;

//		fixed4 _Color;
		float _Frequency;
		float _Amplitude;
		float _Speed;

		float _Diffuse;

		uniform float _uvMaxY;
		uniform float _uvMinY;
		float _waveFactor;

		struct Input {
			float2 uv_MainTex;
            float4 color;
            float4 localVert;
		};

		void vert(inout appdata_full v, out Input o)  
        {  

            UNITY_INITIALIZE_OUTPUT(Input,o);

            o.color = v.color;
            o.localVert = v.vertex;
        } 

		void surf (Input IN, inout SurfaceOutput o) {

			float2 uv = IN.uv_MainTex;

			float offsetx = 0; 

			float2 pos = float2(IN.localVert.x + 1,  IN.localVert.y + 1) * 0.5;

			float minuvY = min(uv.y, _uvMaxY);

			minuvY = clamp(uv.y, _uvMinY, _uvMaxY);

			offsetx = sin((pos.y + _Frequency * _Time.y * _Speed) * _waveFactor) * saturate(_uvMaxY - minuvY) * _Amplitude;

			fixed4 c = tex2D (_MainTex, uv + float2(offsetx, 0));
			fixed4 aCol = tex2D(_AlphaTex, uv + float2(offsetx, 0));

			o.Albedo = c.rgb * _Diffuse;
			o.Alpha = c.a * aCol.r; 
		}
		ENDCG
	}
	FallBack "Diffuse"
}
